//
//  QDTextListModel.m
//  QDMetroClient
//
//  Created by YueWen on 2019/2/15.
//  Copyright © 2019 YueWen. All rights reserved.
//

#import "QDTextListModel.h"
#import "NSDateFormatter+QDShare.h"

@interface QDTextListModel()

@property (nonatomic, copy) NSString *image;
/// 查看数
@property (nonatomic, copy, readwrite) NSString *readString;
/// 显示的时间
@property (nonatomic, copy, readwrite) NSString *date;

@end

@implementation QDTextListModel

+ (NSDictionary *)modelCustomPropertyMapper {
    return @{@"identifier"  : @"id"};
}


- (NSString *)imageUrl {
    return self.image;
}




- (NSString *)readString {
    
    if (_readString) {
        return _readString;
    }
    
    NSInteger read = self.readNum.integerValue;
    // 小于1000直接返回
    if (read < 1000) {  _readString = self.readNum.stringValue; }
    // 介于1k与1w之间
    else if (read < 10000) { _readString = [NSString stringWithFormat:@"%.1fk",read / 1000.0]; }
    // 远大于1w
    else { _readString = [NSString stringWithFormat:@"%.1f万",read / 10000.0]; };
    
    return _readString;
}



- (NSString *)date {
    
    if (_date) {
        return _date;
    }
    
    // 进行是否同年操作
    NSCalendar *calendar = NSCalendar.currentCalendar;
    NSDateComponents *nowCmps = [calendar components:NSCalendarUnitYear fromDate:NSDate.date];
    NSDateComponents *sourceCmps = [calendar components:NSCalendarUnitYear fromDate:[NSDate dateWithTimeIntervalSince1970:self.time.integerValue / 1000]];
    
    // 转换格式
    NSString *formatter = nil;
    
    //进行时间的比对
    if (nowCmps.year == sourceCmps.year) {//表示同一年
//        formatter = @"MM-dd HH:mm";
        formatter = @"MM-dd";
    }else {
//        formatter = @"yyyy-MM-dd HH:mm";
        formatter = @"yyyy-MM-dd";
    }
    
    //进行转换
    _date = [[NSDateFormatter cachedDataFormatterWithString:formatter] stringFromDate:[NSDate dateWithTimeIntervalSince1970:self.time.integerValue / 1000]];
    
    return _date;
}

@end
